module utils {
	export class ServerManager 
	{
		private static _instance: ServerManager;
		public is_requesting:boolean;
		private request:egret.HttpRequest;
		public HostUrl:string = 'http://active.jichuancy.com/index.php/DfXieHui/';
		public constructor() 
		{
			this.init();
		}

		public static get instance(): utils.ServerManager 
        {
            if(!this._instance) 
            {
                this._instance = new utils.ServerManager();
            }
            return this._instance;
        }

		private init():void
		{
			this.is_requesting=false;
			this.request = new egret.HttpRequest();
			this.request.responseType = egret.HttpResponseType.TEXT;
			this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        	this.request.addEventListener(egret.IOErrorEvent.IO_ERROR,this.onPostIOError,this);
        	this.request.addEventListener(egret.ProgressEvent.PROGRESS,this.onPostProgress,this);
		}



		public AddMessage(paramstr:string){
			if(this.is_requesting) return;
			
			this.is_requesting=true;
			var load_url:string = this.HostUrl+'AddMessage';
			this.request.addEventListener(egret.Event.COMPLETE,this.AddMessageBack,this);
			this.request.open(load_url,egret.HttpMethod.POST);
			
        	this.request.send(paramstr);
		}

		private AddMessageBack(event:egret.Event):void {
			this.is_requesting=false;
			this.request.removeEventListener(egret.Event.COMPLETE,this.AddMessageBack,this);
			var resultObj=JSON.parse(this.request.response);
			pub.SignalManager.instance.dispatchSignal(pub.SignalDefin.ADD_MSG_BACK,[{'backdb':resultObj}]);
			
    	}


		public SaveFlagData(flagid:string,flag_title:string){
			if(this.is_requesting) return;
			
			var openid = egret.getOption("userid");
			if(!openid){
				// utils.Toast.instance.showMsg('未授权');
				return;
			}
			this.is_requesting=true;
			var load_url:string = this.HostUrl+'save_flag';
			this.request.addEventListener(egret.Event.COMPLETE,this.SaveFlagDataBack,this);
			this.request.open(load_url,egret.HttpMethod.POST);
			
			var postData:string="openid="+openid+"&flagid="+flagid+"&flag_title="+flag_title;
			console.log('flagid='+flagid);
        	this.request.send(postData);
		}

		private SaveFlagDataBack(event:egret.Event):void {
			this.is_requesting=false;
			this.request.removeEventListener(egret.Event.COMPLETE,this.SaveFlagDataBack,this);
    	}


	    private onPostIOError(event:egret.IOErrorEvent):void {
	        egret.log("post error : " + event);
			this.is_requesting=false;
			utils.Toast.instance.showMsg("请求超时!");
	    }

	    private onPostProgress(event:egret.ProgressEvent):void {
			this.is_requesting=false;
	        egret.log("post progress : " + Math.floor(100*event.bytesLoaded/event.bytesTotal) + "%");
	    }

				// 根据网路地址获取图片的texture
		public getTextureByUrl(url): Promise<any> {
			return new Promise((resolve, reject) => {
				const imgLoader: egret.ImageLoader = new egret.ImageLoader();
				// egret.ImageLoader.crossOrigin = "anonymous";
				const callBack = (event: egret.Event) => {
					if (!event.currentTarget.data) reject({ msg: '图片加载出错~' })
					let texture = new egret.Texture();
					texture.bitmapData = event.currentTarget.data;
					resolve(texture)
				}
				imgLoader.once(egret.Event.COMPLETE, callBack, this);
				imgLoader.load(url);
			})
		}
		
	}
}